package com.wuliu.common.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

/**
 * @author realue
 * @version 1.0
 * @date 2023-08-01 16:51
 */
public class NotLessThanZeroValidator implements ConstraintValidator<NotLessThanZero, Object> {

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (null == value) {
            return true;
        }
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).compareTo(BigDecimal.ZERO) > 0;
        } else if (value instanceof Double) {
            return ((Double) value).compareTo((double) 0) > 0;
        } else if (value instanceof Number) {
            return ((Number) value).intValue() > 0;
        }
        return false;
    }
}
